/**
 * 类服务方式构建比较麻烦 暂定废弃
 */

import { useProvideInject } from "@/hooks/common/useProvide";

// 语言服务的注入令牌
export const Language_Inject_Token = Symbol("LanguageService");
export const Login_Inject_Token = Symbol("LoginService");
export const LoginToggle_Inject_Token = Symbol("Login_Toggle");

export class BaseService {
  constructor() {}
  /**
   * 注册服务
   * @param {*} injectToken - 令牌
   * @param {*} service - 服务
   * @param {*} options - 配置
   */
  static provide(injectToken, service, options = {}) {
    const { createProvide } = useProvideInject();
    const newOptions = Object.assign({}, options, {
      service: true,
      readonly: true,
    });
    createProvide(injectToken, service, newOptions);
  }

  // 提取服务实例
  static inject(injectToken, defaultValue) {
    const { useInject } = useProvideInject();
    return useInject(injectToken, defaultValue);
  }
}

// 语言服务
export class LanguageService extends BaseService {
  static create() {
    return new LanguageService();
  }

  // 注册服务
  static provide() {
    super.provide(Language_Inject_Token, LanguageService);
  }

  // 提取服务实例
  static inject(defaultValue) {
    return super.inject(Language_Inject_Token, defaultValue);
  }

  constructor() {
    super();
    const { language, toggleLanguage } = useLanguage();
    this.language = language;
    this.toggleLanguage = toggleLanguage;
  }

  changeLanguage() {
    // 切换语言
    this.toggleLanguage();
  }
}

export class LoginService extends BaseService {
  static create() {
    return new LoginService();
  }

  // 注册服务
  static provide() {
    super.provide(Login_Inject_Token, LoginService);
  }

  // 提取服务实例
  static inject(defaultValue) {
    return super.inject(Login_Inject_Token, defaultValue);
  }

  login() {
    // 登录
    console.log("登录");
  }
}

export class LoginToggleService extends BaseService {
  static create() {
    return new LoginToggleService();
  }

  constructor() {
    super();
    this._loginService = LoginService.create();
    this._languageService = LanguageService.create();
  }

  // 注册服务
  static provide() {
    super.provide(LoginToggle_Inject_Token, LoginToggleService);
  }

  // 提取服务实例
  static inject(defaultValue) {
    return super.inject(LoginToggle_Inject_Token, defaultValue);
  }

  toggle() {
    // 切换登录状态
    this._loginService.login();
    this._languageService.changeLanguage();
  }
}
